package com.tkjt.pms.calc.utils;

import java.util.HashSet;
import java.util.Set;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import cn.hutool.core.lang.UUID;

public class EsCalcUtils {
	
	public final static String getCBatchCode(String indexYearMonth) {
		return "CBNO"+ indexYearMonth + UUID.randomUUID().hashCode();
	}
	
	public final static String getPGroupCode(String indexYearMonth) {
		return "PGNO"+ indexYearMonth + UUID.randomUUID().hashCode();
	}

	public final static Integer getCalcMi(String variable, String indexYearMonth) {
		String[] tempArray = variable.split("@");
		String strCalcMi = tempArray[0];
		strCalcMi = indexYearMonth.substring(4);
		if(strCalcMi.startsWith("0")) {
			strCalcMi = strCalcMi.substring(1);
		}
		return Integer.parseInt(strCalcMi);
	}

	public final static Integer getDataColumnMi(String dataColumn) {
		dataColumn = dataColumn.toLowerCase();
		if(dataColumn.startsWith("m")) {
			dataColumn = dataColumn.substring(1);
			if(dataColumn.charAt(0)=='0') {
				dataColumn = dataColumn.substring(1);
			}
			return Integer.parseInt(dataColumn);
		}
		return null;
	}

	public final static Integer getDataColumnQi(String dataColumn) {
		dataColumn = dataColumn.toLowerCase();
		if("q".equalsIgnoreCase(dataColumn)) {
			dataColumn = dataColumn.substring(1);
			if(dataColumn.charAt(0)=='0') {
				dataColumn = dataColumn.substring(1);
			}
			return Integer.parseInt(dataColumn);
		}
		return null;
	}

	public final static int getCalcMi(String indexYearMonth) {
		String mStr = indexYearMonth.substring(4);
		if (mStr.charAt(0) == '0') {
			mStr = mStr.substring(1);
		}
		return Integer.valueOf(mStr);
	}

	public final static int getCalcQi(String indexYearMonth) {
		int monthNumber = EsCalcUtils.getCalcMi(indexYearMonth);
		if (monthNumber <= 3) {
			return 1;
		}
		if (monthNumber <= 6) {
			return 2;
		}
		if (monthNumber <= 9) {
			return 3;
		}
		return 4;
	}

	public final static String buildRuleKey(String indexValueCode, String whichData, String indexDepart) {
		StringBuilder sbuf = new StringBuilder();
		sbuf.append(indexValueCode);
		if (StringUtils.isEmpty(whichData) || "*".equals(whichData)) {
			sbuf.append("@").append("");
		} else {
			sbuf.append("@").append(whichData);
		}
		if (StringUtils.isEmpty(indexDepart) || "*".equals(indexDepart)) {
			sbuf.append("@").append("");
		} else {
			sbuf.append("@").append(indexDepart);
		}
		return sbuf.toString();
	}

	public final static String buildRuleKeyOfCommon(String indexValueCode) {
		StringBuilder sbuf = new StringBuilder();
		sbuf.append(indexValueCode).append("@@");
		return sbuf.toString();
	}

	/**
	 * *使用正则表达方式去掉公式中的空格、换行符,去掉公式中的中括号
	 * 
	 * @param valueRule
	 * @return
	 */
	public final static String formatValueRule(String valueRule) {
		return valueRule.replaceAll("\\[|\\]|\\s*|\t|\n", "");
	}

	public final static boolean isVariableChar(char vchar) {
		if (vchar >= 'a' && vchar <= 'z' || vchar >= 'A' && vchar <= 'Z' || vchar >= '0' && vchar <= '9' || vchar == '@') {
			return true;
		}
		return false;
	}

	public final static Set<String> searchOneVariableKey(String varKey, String indexValueRule) {
		Set<String> results = new HashSet<>();
		int indexStartPos = 0;
		boolean canSubString = false;
		while (true) {
			int index = indexValueRule.indexOf(varKey, indexStartPos);
			if (index < 0) {
				break;
			}
			if(index>0) {
				//检查前一个字符是公式类字符，若是，则表示是变量子串
				char tempChar = indexValueRule.charAt(index-1);
				if (EsCalcUtils.isVariableChar(tempChar)) {
					indexStartPos++;
				}else{
					canSubString = true;
				}
			}else{
				canSubString = true;
			}
			if(canSubString){
				for (int searchIndex = index + varKey.length(); searchIndex < indexValueRule.length(); searchIndex++) {
					char tempChar = indexValueRule.charAt(searchIndex);
					if (!EsCalcUtils.isVariableChar(tempChar)) {
						results.add(indexValueRule.substring(index, searchIndex));
						break;
					} else if (EsCalcUtils.isVariableChar(tempChar) && searchIndex == indexValueRule.length() - 1) {
						results.add(indexValueRule.substring(index));
						break;
					}
				}
				indexStartPos = index + varKey.length();
			}
		}

		return results;
	}

}
